package com.example.paimengshop.client_socket;

import com.example.paimengshop.Data;
import com.example.paimengshop.main;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.example.paimengshop.dataclass.*;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

public class ReceiveFromServer
{
    private Socket socket;
    private ObjectInputStream ois;

    public void setSocket(Socket socket)
    {
        this.socket = socket;
    }

    public Socket getSocket()
    {
        return socket;
    }

    public ReceiveFromServer()
    {

    }

    public ReceiveFromServer(Socket socket)
    {
        this.socket = socket;
    }

    public void ReadJsonArray(JSONArray jsonArray)  //在ActionControl_Receive中创建静态数据成员，把值赋给他们
    {
        if(jsonArray.size() == 1)
        {
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            if(jsonObject.containsKey("refresh"))
            {
                if(jsonObject.get("refresh").toString().equals("refresh userInfo"))
                {
                    Data.currentUser = ActionControl_Receive.ReceiveUserInfo(jsonObject);
                    Data.setUserSendOver(true);
                }
            }
            else if(jsonObject.containsKey("login and register"))
            {
                if(jsonObject.get("login and register").toString().equals("login"))
                {
                    ActionControl_Receive.LoginFeedback(jsonObject);
                }
                else if(jsonObject.get("login and register").toString().equals("register"))
                {
                    ActionControl_Receive.RegisterFeedback((jsonObject));
                }
            }
            else if(jsonObject.containsKey("change password"))
            {
                ActionControl_Receive.ChangePasswordFeedback(jsonObject);
            }
            else if(jsonObject.containsKey("communication message"))
            {
                String message = ActionControl_Receive.Communication_Message(jsonObject);


                main.getCustomerServiceController().ShowCommunicateMessage(message);

            }
            else if(jsonObject.containsKey("communication image"))
            {

                File image = ActionControl_Receive.Communication_Image(jsonObject);

                main.getCustomerServiceController().ShowCommunicationImage(image);
            }
        }
        else
        {
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            if(jsonObject.containsKey("all commodity"))
            {
                ArrayList<Commodity> commodityArrayList = ActionControl_Receive.ReceiveCommodityInfo(jsonArray);
                Data.setAllCommodity(commodityArrayList);
                Data.setCommoditySendOver(true);
            }
            else if(jsonObject.containsKey("user order"))
            {
                ArrayList<Order> orderArrayList = ActionControl_Receive.ReceiveOrderInfo(jsonArray);
                Data.setOrders(orderArrayList);
                Data.setOrderSendOver(true);
            }
        }
    }

    public void Receive()  //服务器统一发送JSONArray，客户端统一发送JSONObject
    {
        try
        {
            ois = new ObjectInputStream(socket.getInputStream());
            while(ois != null)
            {
                try
                {
                    JSONArray jsonArray = (JSONArray) ois.readObject();
                    ReadJsonArray(jsonArray);
                }
                catch (EOFException | SocketException e)
                {
                    System.out.println("与服务器的连接断开了");
                    ois = null;
                }
            }
        }
        catch (IOException | ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
